package com.niubi.yygh.user.controller;


import com.niubi.yygh.common.result.R;
import com.niubi.yygh.model.user.UserInfo;
import com.niubi.yygh.user.service.UserInfoService;
import com.niubi.yygh.vo.user.LoginVo;
import com.niubi.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import util.AuthContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 牛俊杰
 * @since 2022-03-24
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoController {
  @Autowired
  private UserInfoService userInfoService;

  @ApiOperation(value = "会员登录")
  @PostMapping("login")
  public R login(@RequestBody LoginVo loginVo) {
    Map<String, Object> info = userInfoService.loginUser(loginVo);
    return R.ok().data(info);
  }

  @PostMapping("auth/userAuth")
  public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
    Long userId = AuthContextHolder.getUserId(request);
    userInfoService.userAuth(userId,userAuthVo);
    return R.ok();
  }

  //获取用户id信息接口
  @GetMapping("auth/getUserInfo")
  public R getUserInfo(HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    UserInfo userInfo = userInfoService.getById(userId);
    return R.ok().data("userInfo",userInfo);
  }
}

